package com.huichaoliu.view;

import java.util.ArrayList;
import java.util.List;

import com.huichaoliu.app.R;
import com.huichaoliu.data.UtilInfo;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class Mpop extends PopupWindow {
	Context context;
	View contenView;
	ListView list;
	int width;
	PopClick click;

	public static final int COST = 0x11;
	public static final int SEASON = 0x12;
	public static final int TYPE = 0x13;

	public Mpop(Context context, int width, PopClick click) {
		super(context);
		this.context = context;
		this.click = click;
		initView();
		this.width = width;
		setWidth(width);
		setHeight(LayoutParams.WRAP_CONTENT);
	}

	@SuppressLint("InflateParams")
	private void initView() {
		contenView = LayoutInflater.from(context).inflate(
				R.layout.mpopupwindow, null, false);
		setContentView(contenView);
		setBackgroundDrawable(new ColorDrawable());
		list = (ListView) contenView.findViewById(R.id.pop_list);
	}

	public void showPop(int type, View v) {
		Madapter adapter = new Madapter(context, type, click);
		adapter.setWidth(width);
		list.setAdapter(adapter);
		showAsDropDown(v);
	}

	public void showPop(int type, View v, List<UtilInfo> listInfo) {
		Madapter adapter = new Madapter(context, type, listInfo, click);
		adapter.setWidth(width);
		list.setAdapter(adapter);
		showAsDropDown(v);
	}

	public interface PopClick {
		void clickCost(String min, String max, String text);

		void clickSeason(String code, String text);

		void clickStyle(String id, String text);
	}

	class Madapter extends BaseAdapter {
		Context context;
		LayoutInflater inflater;
		int type;
		List<UtilInfo> list;
		final String[] _cost = new String[] { "300元以下", "300-500元", "500-800元",
				"800-1000元", "1000-1300元", "1300-1500元", "1500-1800元" };
		final String[] _season = new String[] { "春季", "夏季", "秋季", "冬季" };
		final String[] _type = new String[] { "英伦风", "欧美风", "中国风", "名族风",
				"哥特风", "日韩风", "运动风" };
		PopClick click;

		public Madapter(Context context, int type, PopClick click) {
			this.context = context;
			inflater = LayoutInflater.from(context);
			this.type = type;
			this.click = click;
			initData();
		}

		public Madapter(Context context, int type, List<UtilInfo> list,
				PopClick click) {
			this.context = context;
			inflater = LayoutInflater.from(context);
			this.type = type;
			this.click = click;
			this.list = list;
			// initData();
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@SuppressLint("InflateParams")
		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			Holder mHolder;
			if (arg1 == null || arg1.getTag() == null) {
				mHolder = new Holder();
				arg1 = inflater.inflate(R.layout.pop_item, null);
				mHolder.text = (TextView) arg1.findViewById(R.id.pop_text);
			} else {
				mHolder = (Holder) arg1.getTag();
			}
			mHolder.text.setWidth(width);
			mHolder.text.setText(list.get(arg0).getUserId());
			final int i = arg0;
			final UtilInfo info = list.get(arg0);
			arg1.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					switch (type) {
					case COST:
						String cost = info.getUserId();
						String min = "",
						max = "";
						if (cost.indexOf("-") == -1) {
							min = cost.substring(0, cost.indexOf("元"));
						} else {
							min = cost.substring(0, cost.indexOf("-"));
							max = cost.substring(cost.indexOf("-") + 1,
									cost.length()).replaceAll("元", "");
						}
						if (click != null) {
							click.clickCost(min, max, info.getUserId());
						}
						break;
					case SEASON:
						if (click != null) {
							click.clickSeason(i + 1 + "", info.getUserId());
						}
						break;
					case TYPE:
						if (click != null) {
							click.clickStyle(info.getPicPath(),
									info.getUserId());
						}
						break;

					default:
						break;
					}
				}
			});
			return arg1;
		}

		private void initData() {
			list = new ArrayList<UtilInfo>();
			switch (this.type) {
			case COST:
				for (int i = 0; i < _cost.length; i++) {
					UtilInfo info = new UtilInfo();
					info.setUserId(_cost[i]);
					list.add(info);
				}
				break;
			case SEASON:
				for (int i = 0; i < _season.length; i++) {
					UtilInfo info = new UtilInfo();
					info.setUserId(_season[i]);
					list.add(info);
				}
				break;
			case TYPE:
				for (int i = 0; i < _type.length; i++) {
					UtilInfo info = new UtilInfo();
					info.setUserId(_type[i]);
					list.add(info);
				}
				break;

			default:
				break;
			}
		}

		class Holder {
			TextView text;
		}

		int width;

		public void setWidth(int width) {
			this.width = width;
		}

	}

}
